package com.unnisworld.chess; /**
 * Created by IntelliJ IDEA.
 * User: unni
 * Date: Mar 7, 2009
 * Time: 12:59:25 PM
 * To change this template use File | Settings | File Templates.
 */

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;

public class Square implements Serializable
        {
          protected int left;
          protected int top;
          protected int right;
          protected int bottom;
          protected int color;
          protected Coin coin;
          protected int flag;
          protected int status;

          Square(int tleft,int ttop,int tright,int tbottom,Coin tcoin,int tcolor,int tflag)
              {
               left = tleft;
               top = ttop;
               right = tright;
               bottom = tbottom;
               coin = tcoin;
               color = tcolor;
               flag = tflag;
               status = 0;
              }

          public void paint(Graphics g)
             {
              if(flag == 1)
                 g.setColor(Color.white);
               else
                 g.setColor(Color.GRAY);
              g.fillRect(left,top,right,bottom);

              String prefix = "b_";
              if(color == 0) // black
              {
                 g.setColor(Color.red);
              }
              else {
                 prefix = "w_"; 
                 g.setColor(Color.blue);
              }

              System.out.println("see his "+coin.toString());
              drawCoin(g,left,top, prefix + coin.toString());
              switch(coin)
                {
                 case PAWN:
                   drawPawn(g,left,top,color);
                   break;
                case ROOK:
                   drawRook(g,left,top,color);
                   break;
                case KNIGHT:
                   drawKnight(g,left,top,color);
                   break;
                case BISHOP:
                    drawBishop(g,left,top,color);
                   break;
                 case QUEEN:
                    drawQueen(g,left,top,color);
                    break;
                 case KING:
                    drawKing(g,left,top,color);
                    break;
                 }//end of switch

              g.setColor(Color.red);
              g.drawRect(10,50,400,400);
             }
          public void selected(Graphics g)
             {
              g.setColor(Color.green);
              //System.out.println(top+" "+left);
              g.drawRect(left,top,50,50);
             }
          public Coin getCoin()
             {
              return coin;
             }
         public int getColor()
            {
             return color;
            }
         public void setCoin(Coin tcoin,int tcolor)
            {
             coin = tcoin;
             color = tcolor;
            }
         public void setStatus()
           {
             status = 1;
           }
         public int getStatus()
           {
             return status;
           }

         public void drawCoin(Graphics g,int t,int l,String imageKey)
           {

             g.drawRect(t,l,50,50);
             g.drawImage(ChessBoard.imageMap.get(imageKey), t+10, l, null);
           }

         public void drawPawn(Graphics g,int t,int l,int color)
           {

             g.drawRect(t,l,50,50);
               if(color == 0)
                g.drawImage(ChessBoard.imageMap.get("b_pawn"), t+10, l, null);
               else
                g.drawImage(ChessBoard.imageMap.get("w_pawn"), t+10, l, null);                 
           }

         public void drawRook(Graphics g,int t,int l,int color)
           {
            g.drawRect(t,l,50,50);

               if(color == 0)
                g.drawImage(ChessBoard.imageMap.get("b_rook"), t+10, l, null);
               else
                g.drawImage(ChessBoard.imageMap.get("w_rook"), t+10, l, null);                
           }

         public void drawBishop(Graphics g,int t,int l,int color)
            {
             g.drawRect(t,l,50,50);
               if(color == 0)
                g.drawImage(ChessBoard.imageMap.get("b_bishop"), t+10, l, null);
               else
                g.drawImage(ChessBoard.imageMap.get("w_bishop"), t+10, l, null);                 
            }

        public void drawKnight(Graphics g,int t,int l,int color)
           {
             g.drawRect(t,l,50,50);

               if(color == 0)
                g.drawImage(ChessBoard.imageMap.get("b_knight"), t+10, l, null);
               else
                g.drawImage(ChessBoard.imageMap.get("w_knight"), t+10, l, null);               
           }

        public void drawQueen(Graphics g,int t,int l,int color)
           {
             g.drawRect(t,l,50,50);

               if(color == 0)
                g.drawImage(ChessBoard.imageMap.get("b_queen"), t+10, l, null);
               else
                g.drawImage(ChessBoard.imageMap.get("w_queen"), t+10, l, null);               
           }

         public void drawKing(Graphics g,int t,int l,int color)
           {
               g.drawRect(t,l,50,50);

               if(color == 0)
                g.drawImage(ChessBoard.imageMap.get("b_king"), t+10, l, null);
               else
                g.drawImage(ChessBoard.imageMap.get("w_king"), t+10, l, null);
           }

         }

